local modid = 'webber_the_trainer'

local rewards = require 'core_webber_the_trainer/data/webbert_soul_buy_items'

-- 购买血契商品的请求
AddModRPCHandler(modid,'webbert_soul_buy_item',function (player, index)
    if index ~= nil and not player:HasTag('playerghost') and player.components.webbert_soul then
        local cost = rewards[index].fn_calccost(player.components.webbert_soul['buy_times_'..index])
        local cur = player.components.webbert_soul:GetSoul()
        -- 服务器再次验证拥有的血契数够不够
        if cur >= cost then
            local res = player.components.webbert_soul:TryDelta(-cost)
            if res then
                player.components.webbert_soul['buy_times_'..index] = player.components.webbert_soul['buy_times_'..index] + 1

                -- apply购买函数
                local fn_buy = rewards[index].fn_buy
                if fn_buy ~= nil then
                    fn_buy(player)
                end
            end
        end
    end
end)

-- 轮盘请求
AddModRPCHandler(modid,'rt_panel_click',function (player,which_btn)
    if which_btn ~= nil and not player:HasTag('playerghost') then
        local wp = player.components.inventory and player.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
        if wp and wp.prefab == 'staff_arachnidnexus' then
            for i = 1,8 do
                wp:fn_webbert_rt_panel_active(which_btn)
            end
        end
    end
end)

-- 轮盘 地图传送 点击后 关闭地图
AddClientModRPCHandler(modid,'toggle_minimap',function()
    if ThePlayer and ThePlayer.HUD and ThePlayer.HUD.controls then
        ThePlayer.HUD.controls:ToggleMap()
    end
end)